package com.lzh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.pojo.CT;
import com.lzh.pojo.Class;
import com.lzh.pojo.Teacher;
import com.lzh.service.CTService;
import com.lzh.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

/**
 * @author reborn
 * @create 2020/5/30-19:56
 */
@Controller
public class GlyController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    CTService ctService;

//    前往老师与班级的关系的设置页面
    @RequestMapping("/toSetTC")
    public String toSetCT(@RequestParam(required = true,value = "pageNum",defaultValue = "1")Integer pageNum,
                          @RequestParam(required = true,value = "pageSize",defaultValue = "5")Integer pageSize,
                          int id,Model model){
        PageInfo<Class> classes = ctService.selectCname(pageNum,pageSize,null);
        List<Integer> integers = ctService.selectCidByTid(id);
        Teacher teacher = teacherService.selectTeacherById(id);
        System.out.println(integers);
        System.out.println(teacher);
        model.addAttribute("teacher",teacher);
        model.addAttribute("tcids",integers);
        model.addAttribute("pageInfo",classes);
        model.addAttribute("type", "allG");
        return "gly/setCT";
    }


//    删除教师
    @RequestMapping("/deleteTea")
    public String deleteTea(int id,Model model) {
        Teacher teacher = teacherService.selectTeacherById(id);
        model.addAttribute("teacher",teacher);
        model.addAttribute("iud","删除");
        System.out.println(teacher);
        ctService.deleteCidByTid(id);
        teacherService.deleteTea(id);
        return "teacher/colleague";
    }

    //    添加关系
    @RequestMapping("/addTeatoC")
    public String addTC(@RequestParam(required = true,value = "pageNum",defaultValue = "1")Integer pageNum,
                        @RequestParam(required = true,value = "pageSize",defaultValue = "5")Integer pageSize,
                        int cid,int tid,Model model) {
        CT ct = new CT();
        ct.setC_id(cid);
        ct.setT_id(tid);
        List<Integer> integers = ctService.selectCidByTid(tid);
        for (Integer integer : integers) {
            if (integer==cid){
                toSetCT(pageNum,pageSize,tid,model);
                return "gly/setCT";
            }
        }
        ctService.addTtoC(ct);
        toSetCT(pageNum,pageSize,tid,model);
        return "gly/setCT";
    }


    //    删除老师与班级的联系
    @RequestMapping("/deleteTC")
    public String deleteTC(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                        @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                        int tid,int cid,Model model) {
        CT ct = new CT(cid,tid);
        List<Integer> integers = ctService.selectCidByTid(tid);
        for (Integer integer : integers) {
            if (integer==cid){
                ctService.deleteOCidByTid(ct);
                toSetCT(pageNum,pageSize,tid,model);
                return "gly/setCT";
            }
        }
        toSetCT(pageNum,pageSize,tid,model);
        return "gly/setCT";
    }


    //    查询所有班级
    @RequestMapping("/selectACla")
    public String selectACla(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           Model model) {
        PageInfo<Class> classPageInfo = ctService.selectCname(pageNum, pageSize, null);
        model.addAttribute("pageInfo",classPageInfo);
        model.addAttribute("type","all");
        return "gly/cla";
    }


    //    根据名字查询班级
    @RequestMapping("/selectClaByName")
    public String selectClaByName(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                             String name,Model model) {
        System.out.println(name);
        PageInfo<Class> classPageInfo = ctService.selectClassByName(pageNum, pageSize, name);
        model.addAttribute("pageInfo",classPageInfo);
        model.addAttribute("name",name);
        model.addAttribute("type","name");
        return "gly/cla";
    }


    //    删除班级
    @RequestMapping("/deleteCla")
    public String deleteCla(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           int cid,Model model) {
        List<Class> classes = ctService.selectCname(Arrays.asList(cid));
        for (Class aClass : classes) {
            model.addAttribute("classes",aClass);
        }
        model.addAttribute("bj","删除");
        ctService.deleteStuByCid(cid);
        ctService.delCTBycid(cid);
        ctService.deleteClass(cid);
        selectACla(pageNum,pageSize,model);   //  return "gly/cla";
        return "gly/cla";
    }


    //   添加班级
    @RequestMapping("/AddCla")
    public String deleteCla(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                            Class cla,Model model) {
        model.addAttribute("classes",cla);
        System.out.println(cla);
        ctService.addClass(cla);
        model.addAttribute("bj","添加");
        selectACla(pageNum,pageSize,model);
        return "gly/cla";
    }

    //   去添加页面
    @RequestMapping("/toAddCla")
    public String toAddCla(Class cla,Model model) {

        model.addAttribute("cla",cla);
        model.addAttribute("bj","添加");
        return "gly/addCla";
    }


    //   去设置权限页面
    @RequestMapping("/toSetPerms")
    public String toSetPerms(int id,Model model) {
        Teacher teacher = teacherService.selectTeacherById(id);
        model.addAttribute("teacher",teacher);

        System.out.println(teacher);
        return "gly/setPerms";
    }


    //   设置权限
    @RequestMapping("/setPerms")
    public String SetPerms(Teacher teacher,Model model) {
        System.out.println(teacher);
        teacherService.updateTeacher(teacher);
        System.out.println(teacher);
        model.addAttribute("teacher",teacher);
        return "gly/setPerms";
    }
}
